package com.example.deliveryapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

public class MyAccountPageFragment extends Fragment {
    private String userPhone;
    private TextView PhoneNumber;
    private RelativeLayout log_out;

    public MyAccountPageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.my_account_page, container, false);

        // 初始化手机号文本视图
        PhoneNumber = view.findViewById(R.id.textView_phone_number); // 确保这是正确的 ID

        // 从 SharedPreferences 获取手机号
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        // 将默认值改为 "No login" 而不是 null
        String userPhone = sharedPreferences.getString("userPhone", "No login");
        boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);

        // 如果用户未登录，确保userPhone设置为"No login"
        if (!isLoggedIn) {
            userPhone = "No login";
        }

        // 根据登录状态设置手机号文本
        if (isLoggedIn) {
            PhoneNumber.setText(userPhone); // 用户已登录，显示手机号
        } else {
            PhoneNumber.setText("No login"); // 用户未登录，显示"No login"
        }


        // 手机号点击事件
        PhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLoggedIn) {
                    // 如果已登录，则跳转到个人主页
                    ((MainPageActivity) getActivity()).showMyAccountPageFragment();
                } else {
                    // 如果未登录，则跳转到登录界面
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);
                }
            }
        });

        // 找到登出按钮并设置点击事件
        log_out = view.findViewById(R.id.log_out);
        log_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 清除登录信息
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.remove("userPhone");
                editor.putBoolean("isLoggedIn", false);
                editor.apply();

            }
        });


//        //查看订单(未实现)
//        RelativeLayout your_order = view.findViewById(R.id.your_order);
//        your_order.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(getActivity(), ViewOrderPageActivity.class);
//                startActivity(intent);
//            }
//        });
        return view;

    }
}